package com.whiteape.book.ui.fragment;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.immersionbar.ImmersionBar;
import com.whiteape.book.WaApp;
import com.whiteape.book.contract.BookCategoryContract;
import com.whiteape.book.contract.RecommendContract;
import com.whiteape.book.model.zssq.BookListDetailBean;
import com.whiteape.book.model.zssq.BookSubSortBean;
import com.whiteape.book.model.zssq.SortBookBean;
import com.whiteape.book.model.zssq.packages.BookSortPackage;
import com.whiteape.book.model.zssq.packages.BookSubSortPackage;
import com.whiteape.book.presenter.BookCategoryPresenter;
import com.whiteape.book.ui.activity.BookCategoryDetailActivity;
import com.whiteape.book.ui.adapter.BookSortAdapter;
import com.whiteape.book.ui.base.BaseFragment;
import com.whiteape.book.ui.base.BaseMVPFragment;
import com.whiteape.book.ui.base.BaseTabFragment;
import com.whiteape.book.widget.RefreshLayout;
import com.whiteape.book.widget.itemdecoration.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import com.whiteape.book.R;

public class RecommendCategoryChildFragment extends BaseMVPFragment<BookCategoryContract.Presenter> implements BookCategoryContract.View{


    @BindView(R.id.book_sort_rl_refresh)
    RefreshLayout mRlRefresh;

    @BindView(R.id.rv_book_sort)
    RecyclerView mRvSort;

    private static final int SPAN_COUNT = 3;
    private BookSortAdapter mAdapter;
    private BookSubSortPackage mSubSortPackage;

    private String tag = "male";

    private static final String KEY_TAG ="tag";
    public static Fragment newInstance(String tag) {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TAG, tag);
        Fragment fragment = new RecommendCategoryChildFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        if(getArguments() != null && getArguments().containsKey(KEY_TAG)){
            tag = getArguments().getString(KEY_TAG);
        }else if(savedInstanceState != null){
            tag = savedInstanceState.getString(KEY_TAG);
        }
    }

    @Override
    protected int getContentId() {
        return R.layout.fragment_recommend_category_child;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mAdapter.setOnItemClickListener(
                (view,pos) -> {
                    BookSubSortBean subSortBean = tag.equals("male") ? mSubSortPackage.getMale().get(pos) : mSubSortPackage.getFemale().get(pos);
                    BookCategoryDetailActivity.startActivity(getContext(),tag,subSortBean);
                }
        );
    }

    private void setUpAdapter(){
        mAdapter = new BookSortAdapter();

        mRvSort.setLayoutManager(new GridLayoutManager(getContext(),SPAN_COUNT));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            RecyclerView.ItemDecoration itemDecoration = new DividerGridItemDecoration(getContext(), R.drawable.shape_divider_row, R.drawable.shape_divider_col);
            mRvSort.addItemDecoration(itemDecoration);
        }

        mRvSort.setAdapter(mAdapter);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setUpAdapter();
    }

    @Override
    protected BookCategoryContract.Presenter bindPresenter() {
        return new BookCategoryPresenter();
    }

    @Override
    protected void startLogic() {
        super.startLogic();
        mRlRefresh.showLoading();
        mPresenter.refreshSortBean();
    }


    @Override
    public void finishRefresh(BookSortPackage sortPackage, BookSubSortPackage subSortPackage) {

        if (sortPackage == null || sortPackage.getMale().size() == 0 || sortPackage.getFemale().size() == 0){
            mRlRefresh.showEmpty();
        }
        else {
            if(tag.equals("male")) {
                mAdapter.refreshItems(sortPackage.getMale());
            }else{
                mAdapter.refreshItems(sortPackage.getFemale());
            }
        }
        mSubSortPackage = subSortPackage;
    }

    @Override
    public void showLoadError() {
        mRlRefresh.showError();
    }

    @Override
    public void showError() {
        mRlRefresh.showError();

    }

    @Override
    public void complete() {
        mRlRefresh.showFinish();
    }
}
